#pragma once

#include<Windows.h>
#include<iostream>
#include<time.h>
#include<math.h>
#include<list>

//Khai bao directX
#define DIRECTINPUT_VERSION 0X0800
#include<d3d9.h>
#include<d3dx9.h>
#include<DxErr.h>
#include<dinput.h>

//Khai bao thu vien khoi tao
#include"Timer.h"
#include"Texture.h"
#include"Entity.h"
#include"Sprite.h"
#include"Input.h"
#include"Camera.h"

extern bool game_over;
extern bool game_preload();
extern bool game_init();
extern void game_update();
extern void game_render2D();
extern void game_end();

extern void game_keyPress(int key);
extern void game_keyRelease(int key);

extern void game_entityUpdate(Advanced2D::Entity*);
extern void game_entityRender(Advanced2D::Entity*);

namespace Advanced2D{
	class Engine{
	private:
		HWND p_windowHandle;
		LPDIRECT3D9 p_d3d;
		LPDIRECT3DDEVICE9 p_device;
		LPDIRECT3DSURFACE9 p_backbuffer;
		LPD3DXSPRITE p_spritehandler;

		bool p_fullscreen;
		std::string p_apptitle;
		int p_screenwidth;
		int p_screenheight;
		int p_colordepth;

		Input* p_input;						
		std::list<Entity*> p_entities;

	public:
		Engine();
		virtual ~Engine();						
		
		//Init
		bool Init(int width, int height, int colordepth, bool fullscreen);
		//Update
		void Update();
		void ClearScene(D3DCOLOR);
		void ExitGame();
		//Close
		void Close();

		//Input
		void UpdateKeyboard();
		
		//Entites
		void UpdateEntities();
		void DrawEntities();
		void DeleteEntities();

		void addEntity(Entity* entity);
		Entity* findEntity(int objectType);

		void setWindowHandle(HWND hWnd){ p_windowHandle = hWnd; }
		HWND getWindowHandle(){ return p_windowHandle; }
		LPDIRECT3DDEVICE9 getDevice(){ return p_device; }
		LPDIRECT3DSURFACE9 getBackBuffer(){ return p_backbuffer;}
		LPD3DXSPRITE getSpriteHandler(){ return p_spritehandler;}				

		bool getFullScreen(){ return p_fullscreen; }
		void setFullScreen(bool value){ p_fullscreen = value; }
		std::string getAppTitle(){ return p_apptitle; }
		void setAppTitle(std::string title){ p_apptitle = title; }
		int getScreenWidth(){ return p_screenwidth;}
		void setScreenWidth(int value){ p_screenwidth = value; }
		int getScreenHeight(){ return p_screenheight; }
		void setScreenHeight(int value){ p_screenheight = value;}
		int getColorDepth(){ return p_colordepth; }
		void setColorDepth(int value){ p_colordepth = value; }
		
		
	};
};

extern Advanced2D::Engine* g_engine;
extern Advanced2D::Camera* g_camera;